import pygame
import math

# 初始化Pygame
pygame.init()

# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)

# 设置屏幕尺寸
size = (1000, 1000)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("正弦与余弦示意图")

# 循环直至用户关闭窗口
done = False
clock = pygame.time.Clock()

angle = 0  # 初始角度

while not done:
    for event in pygame.event.get():  # 用户操作
        if event.type == pygame.QUIT:  # 如果用户关闭窗口
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                angle -= 5  # 左箭头减小角度
            elif event.key == pygame.K_RIGHT:
                angle += 5  # 右箭头增大角度

    # 清屏
    screen.fill(WHITE)

    # 计算单位圆上的点
    center_x, center_y = size[0] // 2, size[1] // 2
    radius = 400
    angle_rad = math.radians(angle)

    # 绘制坐标轴
    pygame.draw.line(screen, BLACK, (center_x, 100), (center_x, 900), 3)
    pygame.draw.line(screen, BLACK, (100, center_y), (900, center_y), 3)

    # 正弦和余弦计算
    cos_value = math.cos(angle_rad)
    sin_value = math.sin(angle_rad)

    # 单位圆上对应的X, Y坐标
    point_x = center_x + int(radius * cos_value)
    point_y = center_y - int(radius * sin_value)

    # 绘制单位圆
    pygame.draw.circle(screen, BLACK, (center_x, center_y), radius, 3)

    # 绘制角度线
    pygame.draw.line(screen, BLACK, (center_x, center_y), (point_x, point_y), 3)

    # 标出正弦值
    pygame.draw.line(screen, RED, (point_x, point_y), (point_x, center_y), 3)

    # 标出余弦值
    pygame.draw.line(screen, BLUE, (point_x, point_y), (center_x, point_y), 3)

    # 更新屏幕
    pygame.display.flip()

    clock.tick(60)

pygame.quit()
